home *** CD-ROM | disk | FTP | other *** search
/ Tux Racer / Tux Racer.iso / program files / Sunspire Studios / Tux Racer / squid_init.tcl < prev    next >
Encoding:
Text File  |  2001-08-21  |  8.6 KB  |  382 lines

  1. # -*-tcl-*-
  2. # Initialization script for Squid Engine
  3.  
  4. if { ![info exists tux_data_dir] } {
  5.     set tux_data_dir [pwd]
  6. }
  7.  
  8. #---------------------------------------------------------------------------
  9. #
  10. # Library procedures
  11. #
  12. #---------------------------------------------------------------------------
  13.  
  14. #---------------------------------------------------------------------------
  15. #
  16. # Returns list of all classes from which object is derived, starting with 
  17. # top-level ancestor (i.e., s_object)
  18. #
  19. proc objclasslist { { obj . } } {
  20.     set objclass [objget $obj class]
  21.     set curclass $objclass
  22.     set classlist [list $objclass]
  23.  
  24.     while {1} {
  25.     set curclass [objget $curclass ancestor]
  26.     if { $curclass == "null" } {
  27.         break;
  28.     }
  29.     set classlist [concat $curclass $classlist]
  30.     }
  31.     return $classlist
  32. }
  33.  
  34. #---------------------------------------------------------------------------
  35. #
  36. # 'ls'-like function for objects
  37. #
  38. proc objls { {obj .} } {
  39.     # Make sure obj is a container
  40.     if { ![objcall $obj is_a s_container] } {
  41.     error "'$obj' is not an s_container"
  42.     }
  43.  
  44.     set children [objget $obj children]
  45.  
  46.     set new_children [list]
  47.     foreach child $children {
  48.     set child [objget $child basename]
  49.     lappend new_children $child
  50.     }
  51.  
  52.     set new_children [lsort -ascii $new_children]
  53.  
  54.     return $new_children
  55. }
  56.  
  57. #---------------------------------------------------------------------------
  58. #
  59. # Executes objset in a catch block.
  60. #
  61. proc objtryset { obj args } {
  62.     set cmd [concat [list objset $obj ] $args ]
  63.     if {[catch $cmd msg ] } { 
  64.     tux_warning "Warning: error during\n$cmd\n$msg" 
  65.     }
  66. }
  67.  
  68. #---------------------------------------------------------------------------
  69. #
  70. # Returns code which will restore object to its current state
  71. #
  72. proc objserialize { {obj .} } {
  73.  
  74.     if { ![objget $obj serializable] } {
  75.     # Object isn't serializable
  76.     return
  77.     }
  78.  
  79.     set basename [objget $obj basename]
  80.     set class [objget $obj class]
  81.     set parent [objget $obj parent]
  82.     set properties [objget $class:properties children]
  83.  
  84.     # Create a new instance of object so we can compare current property 
  85.     # values to default values.
  86.     if { ![objexists :tmp] } {
  87.     objnew s_container : tmp
  88.     }
  89.  
  90.     set suffix 0
  91.     while {1} {
  92.     set protoname "[objget $class basename]-$suffix"
  93.     if { ![objexists ":tmp:$protoname"] } {
  94.         set prototype [objnew $class :tmp $protoname]
  95.         break
  96.     } else {
  97.         incr suffix
  98.     }
  99.     }
  100.  
  101.     set setcode "objcreate {[objget $class basename]} {$obj}"
  102.  
  103.     foreach prop $properties {
  104.     set access [objget $prop access]
  105.  
  106.     if { $access != "rw" } {
  107.         # Can't serialize using this property...
  108.         continue
  109.     }
  110.  
  111.     set propname [objget $prop basename]
  112.  
  113.     set value [objget $obj $propname]
  114.  
  115.     if { "$value" != "[objget $prototype $propname]" } {
  116.  
  117.         set setcode  "$setcode \\\n    {-$propname} {$value}"
  118.     }
  119.     }
  120.  
  121.     objdel $prototype
  122.  
  123.     if { [objcall $obj is_a s_container] && \
  124.      [objget $obj serialize_children] } {
  125.     foreach child [objget $obj children] {
  126.         set setcode "$setcode\n\n[objserialize $child]"
  127.     }
  128.     }
  129.  
  130.     return $setcode
  131. }
  132.  
  133. #---------------------------------------------------------------------------
  134. #
  135. # Copies an object.
  136. #
  137. proc objcp { obj newparent newname } {
  138.  
  139.     if { ![objexists $obj] } {
  140.     error "$obj does not exist"
  141.     }
  142.  
  143.     if { ![objget $obj serializable] } {
  144.     tux_warning "$obj is not serializable, skipping..."
  145.     return
  146.     }
  147.  
  148.     if { ![objexists $newparent] } {
  149.     error "$newparent does not exist"
  150.     }
  151.  
  152.     set newobj "$newparent:$newname"
  153.  
  154.     if { [objexists $newobj] } {
  155.     error "$newobj already exists"
  156.     }
  157.  
  158.     # Copy object
  159.     set class [objget $obj class]
  160.  
  161.     objnew $class $newparent $newname
  162.  
  163.     set properties [objget $class:properties children]
  164.  
  165.     foreach prop $properties {
  166.     set access [objget $prop access]
  167.     set propname [objget $prop basename]
  168.  
  169.     if { $access != "rw" } {
  170.         continue
  171.     }
  172.  
  173.     objset $newobj -$propname [objget $obj $propname]
  174.     }
  175.  
  176.     if [objcall $obj is_a s_container] {
  177.     # Copy children
  178.     foreach child [objget $obj children] {
  179.         objcp $child $newobj [objget $child basename]
  180.     }
  181.     }
  182. }
  183.  
  184. #---------------------------------------------------------------------------
  185. #
  186. # objreset -- resets the r/w properties of an object to their default 
  187. # settings
  188. #
  189. proc objreset { obj } {
  190.     set suffix 0
  191.  
  192.     if { ![objexists $obj] } {
  193.     error "$obj does not exist"
  194.     }
  195.  
  196.     if { ![objexists :tmp] } {
  197.     objnew s_container : tmp
  198.     }
  199.  
  200.     set class [objget $obj class]
  201.  
  202.     while {1} {
  203.     set protoname "[objget $class basename]-$suffix"
  204.     if { ![objexists ":tmp:$protoname"] } {
  205.         set prototype [objnew $class :tmp $protoname]
  206.         break
  207.     } else {
  208.         incr suffix
  209.     }
  210.     }
  211.  
  212.     set props [objget "$class:properties" children]
  213.     foreach prop $props {
  214.     set propname [objget $prop basename]
  215.     set access [objget $prop access]
  216.  
  217.     if {$access == "rw"} {
  218.         objset $obj -$propname [objget $prototype $propname]
  219.     }
  220.     }
  221.  
  222.     objdel $prototype
  223. }
  224.  
  225. #---------------------------------------------------------------------------
  226. #
  227. # objcreate -- creates (if necessary) and initializes a new object
  228. #
  229. proc objcreate { class obj args } {
  230.     
  231.     if { ![regexp {^(.*):([^:]*)$} $obj dummy parent name] } {
  232.     set parent .
  233.     set name $obj
  234.     }
  235.     if { "$parent" == "" } {
  236.     set parent ":"
  237.     }
  238.  
  239.     if [objexists $obj] {
  240.     set curclass [objget $obj class]
  241.  
  242.     if { $curclass != "$class" && \
  243.         [objget $curclass basename] != "$class" } {
  244.         error "$obj already exists and is of the wrong class"
  245.     }
  246.  
  247.     set class $curclass
  248.  
  249.     #
  250.     # Reset properties not specified in args
  251.     #
  252.  
  253.     # Figure out which properties have been set
  254.     array set setprops {}
  255.     for {set i 0} {$i<[llength $args]} {incr i 2} {
  256.         set setprops([lindex $args $i]) 1
  257.     }
  258.  
  259.     if { ![objexists :tmp] } {
  260.         objnew s_container : tmp
  261.     }
  262.     
  263.     # Create a "prototype object", from which we get defaults
  264.     set suffix 0
  265.     while {1} {
  266.         set protoname "[objget $class basename]-$suffix"
  267.         if { ![objexists ":tmp:$protoname"] } {
  268.         set prototype [objnew $class :tmp $protoname]
  269.         break
  270.         } else {
  271.         incr suffix
  272.         }
  273.     }
  274.  
  275.     # Now we reset properties not in $args
  276.     foreach prop [objget $class:properties children] {
  277.         set propname [objget $prop basename]
  278.         set access [objget $prop access]
  279.  
  280.         if [info exists setprops("-$propname")] {
  281.         # This is set in $args, don't need to reset
  282.         continue
  283.         }
  284.  
  285.         if { $access == "rw" } {
  286.         objset $obj -$propname [objget $prototype $propname]
  287.         }
  288.     }
  289.  
  290.     objdel $prototype
  291.     } else {
  292.     if [catch {objnew $class $parent $name} msg] {
  293.         error "could not create $obj: $msg"
  294.     }
  295.     }
  296.  
  297.     if { [llength $args] > 0 } {
  298.     eval [concat [list objtryset $obj] $args]
  299.     }
  300. }
  301.  
  302.  
  303. #---------------------------------------------------------------------------
  304. #
  305. # Creates a unique object name given a parent and a prefix
  306. #
  307. proc objuniquename { parent prefix } {
  308.     set suffix 0
  309.     set name $prefix
  310.     while {1} {
  311.     if { ![objexists "$parent:$name"] } {
  312.         break
  313.     } else {
  314.         incr suffix
  315.     }
  316.     set name "$prefix-$suffix"
  317.     }
  318.  
  319.     return $name
  320. }
  321.  
  322.  
  323. #---------------------------------------------------------------------------
  324. #
  325. # Returns true iff pkg is loaded
  326. #
  327. proc IsPackageLoaded { pkg } {
  328.     set pkgnames [package names]
  329.     return [expr [lsearch $pkgnames $pkg] >= 0]
  330. }
  331.  
  332.  
  333. #---------------------------------------------------------------------------
  334. #
  335. # Wraps callbacks in the proper namespace.
  336. #
  337. # Based on example 14-4, p 171 in _Practical Programming in Tcl and
  338. # Tk_, 2nd Ed. by Brent B. Welch.
  339. #
  340. proc code { args } {
  341.     set namespace [uplevel {namespace current}]
  342.     return [list namespace inscope $namespace $args]
  343. }
  344.  
  345. #---------------------------------------------------------------------------
  346. #
  347. # min and max functions
  348. #
  349. proc min { a b } {
  350.     if { $a < $b } {
  351.     return $a
  352.     }
  353.     return $b
  354. }
  355.  
  356. proc max { a b } {
  357.     if { $a > $b } {
  358.     return $a
  359.     }
  360.     return $b
  361. }
  362.  
  363.  
  364. #---------------------------------------------------------------------------
  365. #
  366. # Source our Tcl library files
  367. #
  368. #---------------------------------------------------------------------------
  369. source "tcllib/quadtree.tcl"
  370. source "tcllib/saveheights.tcl"
  371. source "tcllib/sounds.tcl"
  372.  
  373.  
  374. #---------------------------------------------------------------------------
  375. #
  376. # Source Tk-related files if Tk is loaded
  377. #
  378. #---------------------------------------------------------------------------
  379. if { [IsPackageLoaded Tk] } {
  380.     source tk_init.tcl
  381. }
  382.